/*
 * ConsultarProjetos.java
 *
 * Created on 6 de Julho de 2008, 19:49
 */

package gerenciadorprojetos.forms;

import gerenciadorprojetos.GerenciadorProjetosApp;
import gerenciadorprojetos.services.ProjetoService;
import gerenciadorprojetos.services.StatusService;
import gerenciadorprojetos.services.UsuarioService;
import org.jdesktop.application.Action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.ListIterator;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.apache.log4j.Logger;

/**
 *
 * @author  eduardo
 */
public class ConsultarProjetos extends javax.swing.JDialog {
    
    private ApplicationContext context;

    /** Creates new form ConsultarProjetos */
   
    public ConsultarProjetos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        inicializaFormulario();
    }
    
    @SuppressWarnings("empty-statement")
    public void inicializaFormulario() {
        context = new ClassPathXmlApplicationContext("spring-servlet.xml");
        
        UsuarioService uDAO = (UsuarioService)context.getBean("usuarioService");
        jComboBoxExecutor.removeAllItems();
        
        List<String> l = uDAO.getUsuarios();
        ListIterator<String> it = l.listIterator();
        do
        {
            jComboBoxExecutor.addItem(it.next());
        } while(it.hasNext());
        
        StatusService sDAO = (StatusService)context.getBean("statusService");
        jComboBoxStatus.removeAllItems();
        
        jComboBoxStatus.addItem("TODOS");
        l = sDAO.getAllStatus();
        
        for(it = l.listIterator(); it.hasNext(); jComboBoxStatus.addItem(it.next()));

        jListProjetos.setEnabled(false);
        jListProjetos.setVisible(false);
        
        jButtonEditar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemCriarNota = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabelStatus = new javax.swing.JLabel();
        jLabelExecutor = new javax.swing.JLabel();
        jComboBoxStatus = new javax.swing.JComboBox();
        jComboBoxExecutor = new javax.swing.JComboBox();
        jButtonPesquisar = new javax.swing.JButton();
        jButtonSair = new javax.swing.JButton();
        jButtonEditar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListProjetos = new javax.swing.JList();

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getActionMap(ConsultarProjetos.class, this);
        jMenuItemCriarNota.setAction(actionMap.get("ExibeFormCriarNotaAction")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getResourceMap(ConsultarProjetos.class);
        jMenuItemCriarNota.setText(resourceMap.getString("jMenuItemCriarNota.text")); // NOI18N
        jMenuItemCriarNota.setName("jMenuItemCriarNota"); // NOI18N
        jPopupMenu1.add(jMenuItemCriarNota);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabelStatus.setText(resourceMap.getString("jLabelStatus.text")); // NOI18N
        jLabelStatus.setName("jLabelStatus"); // NOI18N

        jLabelExecutor.setText(resourceMap.getString("jLabelExecutor.text")); // NOI18N
        jLabelExecutor.setName("jLabelExecutor"); // NOI18N

        jComboBoxStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos" }));
        jComboBoxStatus.setName("jComboBoxStatus"); // NOI18N

        jComboBoxExecutor.setName("jComboBoxExecutor"); // NOI18N

        jButtonPesquisar.setAction(actionMap.get("Pesquisar")); // NOI18N
        jButtonPesquisar.setName("jButtonPesquisar"); // NOI18N

        jButtonSair.setAction(actionMap.get("Sair")); // NOI18N
        jButtonSair.setName("jButtonSair"); // NOI18N

        jButtonEditar.setAction(actionMap.get("Editar")); // NOI18N
        jButtonEditar.setName("jButtonEditar"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                        .addComponent(jButtonSair, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelStatus)
                            .addComponent(jLabelExecutor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxExecutor, 0, 523, Short.MAX_VALUE)
                            .addComponent(jComboBoxStatus, 0, 523, Short.MAX_VALUE))))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonEditar, jButtonPesquisar, jButtonSair});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelStatus)
                    .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelExecutor)
                    .addComponent(jComboBoxExecutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonPesquisar)
                    .addComponent(jButtonSair)
                    .addComponent(jButtonEditar))
                .addContainerGap())
        );

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListProjetos.setEnabled(false);
        jListProjetos.setName("jListProjetos"); // NOI18N
        jListProjetos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListProjetosMouseClicked(evt);
            }
        });
        jListProjetos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListProjetosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListProjetos);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jListProjetosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListProjetosValueChanged
    jButtonEditar.setEnabled(true);
}//GEN-LAST:event_jListProjetosValueChanged

private void jListProjetosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListProjetosMouseClicked
    if (evt.getButton() == evt.BUTTON1 && evt.getClickCount() >= 2) {
        Editar();
    }

    if (evt.getButton() == evt.BUTTON3) {
        jPopupMenu1.show(jListProjetos, evt.getX(), evt.getY());
    }
}//GEN-LAST:event_jListProjetosMouseClicked

    @Action
    public void Pesquisar() {
        ProjetoService pS = (ProjetoService)context.getBean("projetoService");
        
        String usuario = (String)jComboBoxExecutor.getSelectedItem();
        String status = (String)jComboBoxStatus.getSelectedItem();
        
        DefaultListModel model = new DefaultListModel();

        if (status.equals("TODOS")) {
            status = null;
        }

        List<String> l = pS.getProjetos(status, usuario);
        ListIterator<String> it = l.listIterator();

        while(it.hasNext())
        {
            try
            {
                model.addElement(it.next());
            }
            catch(Exception e)
            {
                logger.error((new StringBuilder()).append("Erro ao adicionar um elemento na lista de projetos ! - Mensagem: ").append(e.getMessage()).toString());
            }
        }
        jListProjetos.setModel(model);
        jListProjetos.setVisible(true);
        jListProjetos.setEnabled(true);
        jButtonEditar.setEnabled(false);
    }

    @Action
    public void Sair() {
        setVisible(false);
    }

    @Action
    public void Editar() {
        if (editar == null) {
            JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            editar = new EditarProjeto(mainFrame,true);
            criarNota.setResizable(false);
            editar.setLocationRelativeTo(mainFrame);
        }
        ((EditarProjeto)editar).inicializaFormulario((String)jListProjetos.getSelectedValue());
        GerenciadorProjetosApp.getApplication().show(editar);
        
        this.Pesquisar();
    }

    @Action
    public void ExibeFormCriarNotaAction() {
        if (criarNota == null) {
            JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            criarNota = new CriarNota(mainFrame,true);
            criarNota.setResizable(false);
            criarNota.setLocationRelativeTo(mainFrame);
        }
        ((CriarNota)criarNota).setProjeto((String)jListProjetos.getSelectedValue());
        ((CriarNota)criarNota).inicializaForm();
        GerenciadorProjetosApp.getApplication().show(criarNota);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonPesquisar;
    private javax.swing.JButton jButtonSair;
    private javax.swing.JComboBox jComboBoxExecutor;
    private javax.swing.JComboBox jComboBoxStatus;
    private javax.swing.JLabel jLabelExecutor;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JList jListProjetos;
    private javax.swing.JMenuItem jMenuItemCriarNota;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private Logger logger = Logger.getLogger(ConsultarProjetos.class);
    private JDialog editar;
    private JDialog criarNota;

    
}
